package com.ad.admin.andriodthread;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

public class MusicService extends Service {
    int process = 0;
    private boolean isStop = false;
    public MusicService() {
    }


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        int action = intent.getIntExtra("action",-1);
        switch (action){
            case 1:
                play();
                break;
            case 2:
                pause();
                break;
            case 3:
                resume();
                break;
            default:
                break;

        }

        return super.onStartCommand(intent, flags, startId);

    }

    public void play(){
        Thread th = new Thread(){
            public void run() {

                synchronized (MusicService.this){

                    while ( process < 800){
                        Log.i("gtq","service........"+Thread.currentThread().getName()+"process..."+process);
                        if (isStop){
                            try {
                                MusicService.this.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                        process++;
                    }

                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }

        };
        th.start();

    }
    public void pause(){
        isStop = true;
    }
    public synchronized void resume(){
        isStop = false;
        notify();
    }
    class MyBinder extends Binder{
        public int getProcess(){
            return process;
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        return new MyBinder();
    }
}
